/**
 * Copyright 2009-2015. qiuxj. All rights reserved. <a>http://www.firebird.com</a>
 */
package com.firebird.commons.util;

/**
 * @author qiuxj create on 2009-3-22
 *
 */
public class StringUtil {
	
	/**
	 * subString String Object By Byte
	 * @param str  >>String object
	 * @param subLength >>return object's length
	 * @return
	 */
	public static String subStringByByte(String str, int subLength) {
		if (str == null) {
			return str;
		}
		int sl = str.length();
		if (sl <= subLength) {
			return str;
		}
		int maxCount = subLength * 2;
		int count = 0;
		int i = 0;
		while (count < maxCount && i < sl) {
			if (str.codePointAt(i) < 256) {
				count++;
			} else {
				count += 2;
			}
			i++;
		}
		if (count > maxCount) {
			i--;
		}
		return str.substring(0, i);
	}
	
	/**
	 * subString String Object By Byte and endWith a String
	 * @param str >>a String Object
	 * @param subLength >>the String Object how were you subString length
	 * @param endStr >>return's Object endWith this
	 * @return
	 */
	public static String subStringByByte(String str,int subLength,String endStr){
		subLength -= 1;
		return subStringByByte(str, subLength)+endStr;
	}
	
	/**
	 * subString String Object By Byte and endWith "…"
	 * @param str >>a String Object
	 * @param subLength >>the String Object how were you subString length
	 * @return
	 */
	public static String subStringEndWithPoint(String str,int subLength){
		return subStringByByte(str, subLength, "…");
	}
	
	/**
	 * Count the String's byte number of GBK enCode's String
	 * @param s
	 * @return
	 */
	public static int countStringByte(String s) {
		if (s == null) {
			return 0;
		}
		int count = 0;
		for (int i = 0; i < s.length(); i++) {
			if (s.codePointAt(i) < 256) {
				count++;
			} else {
				count += 2;
			}
		}
		return count;
	}
	
	/**
	 * validation the String has Chinese Code
	 * @param s
	 * @return
	 */
	public static boolean hasChinese(String s) {
		if (s == null) {
			return false;
		}
		return countStringByte(s) > s.length();
	}
	
	/**
	 * convert HtmlString To TxtString
	 * @param htm
	 * @return
	 */
	public static String convertHtmToTxt(String htm) {
		if (htm == null) {
			return htm;
		}
		htm = htm.replace("&amp;", "&");
		htm = htm.replace("&lt;", "<");
		htm = htm.replace("&gt;", ">");
		htm = htm.replace("&quot;", "\"");
		htm = htm.replace("&nbsp;", " ");
		htm = htm.replace("<br/>", "\n");
		return htm;
	}
	
	/**
	 * trim a String Object
	 * @param str
	 * @return
	 */
	public static String trim(String str) {
		if(str == null){
			return str;
		}
		try {
			str = str.trim();
		} catch (Exception e) {
		}
		return str;
	}
	
	/**
	 * validation a String Object is null or ""
	 * @param str
	 * @return
	 */
	public static Boolean isEmpty(String str){
		Boolean result = false;
		if(str == null || str.length() == 0){
			result = true;
		}
		return result;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
